/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.activation.domain.PackageRedemptionLines;
import com.cap.bts.crmdp.dealer.coupon.dao.ICouponDao;
import com.cap.bts.crmdp.dealer.coupon.dao.ICouponRedeemDao;
import com.cap.bts.crmdp.dealer.coupon.domain.Coupon;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponRedeem;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponForMARSDto;
import com.cap.bts.crmdp.dealer.mars.dto.ItemTypeInfoDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryBySqlJpaCallback;
import com.cap.bts.framework.common.dao.callback.QueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;

/**
 *优惠券
 */
@Repository("couponRedeemDao")
public class CouponRedeemDaoImpl extends BaseDaoImpl<CouponRedeem, Long> implements ICouponRedeemDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CouponRedeemDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.CouponRedeem") Class<CouponRedeem> clazz) {
        super(clazz);
    }


    @Override
    public void settlement(List<String> couponNoList, String soNumber)throws AppException{
    	String sql = "UPDATE tm_raw_coupon_redeem  " +
    			" SET " +
    			" MARS_LOCK_STATUS = '0',  " +
    			" SO_NUMBER_SETTLE = '"+ soNumber +"' where id in (";
    	
    	for (int i = 0; i < couponNoList.size(); i++) {
    		if(i > 0){
    			sql += ",";
    		}
			sql += couponNoList.get(i);
		}
    	
    	sql += ")";
    	
    	super.getEntityManager().createNativeQuery(sql).executeUpdate();
    }
    
    @Override
    public void cancelOrder(List<String> couponNoList, String soNumber)throws AppException{
    	
    	String sql = "delete from tm_raw_coupon_redeem  " +
    			" where id in (";
    	for (int i = 0; i < couponNoList.size(); i++) {
    		if(i > 0){
    			sql += ",";
    		}
			sql +=  couponNoList.get(i);
		}
    	
    	sql += ")";
    	
    	super.getEntityManager().createNativeQuery(sql).executeUpdate();
    	
    }
    
    @Override
	public List<CouponRedeem> getByCouponNumber(String couponNumber) throws AppException{
    	String hql = "from CouponRedeem a where a.couponNumber = '" + couponNumber + "'";
    	
        return this.getJpaTemplate().execute(new QueryJpaCallback<CouponRedeem>(hql));
    }
    
}
